/**
 * Copyright (c) 2019 xstgongyi.org
 * All rights reserved.
 */
package org.xstgongyi.eduaid.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xstgongyi.eduaid.common.ControllerHelper;
import org.xstgongyi.eduaid.dataobject.ProjectPhaseDO;
import org.xstgongyi.eduaid.bo.ProjectPhaseStatus;
import org.xstgongyi.eduaid.service.ProjectService;
import org.xstgongyi.eduaid.service.SemesterService;
import org.xstgongyi.eduaid.service.ProjectPhaseService;

/**
 * @author oldcrane<br/>
 * @created 2019-04-03
 */
@Controller
@RequestMapping("/project-phases")
public class ProjectPhaseController {

	@Autowired
	private ProjectPhaseService projectPhaseService;

	@Autowired
	private ProjectService projectService;

	@Autowired
	private SemesterService semesterService;

	@GetMapping("")
	@ResponseBody
	public Map<String, Object> list(@RequestParam(name = "projectId", required = false) Integer projectId,
			@RequestParam(name = "semesterId", required = false) Integer semesterId,
			@RequestParam(name = "status", required = false) ProjectPhaseStatus status) {

		// List<ProjectPhaseDO> projectPhaseDOList =
		// projectPhaseService.listByProject(projectId);
		List<ProjectPhaseDO> projectPhaseDOList = projectPhaseService.list(projectId, semesterId, status);

		List<ProjectPhaseVO> voList = new ArrayList<>();
		for (ProjectPhaseDO item : projectPhaseDOList) {
			ProjectPhaseVO vo = ProjectPhaseVO.of(item);
			vo.addProjectInfo(projectService.getById(item.getProjectId()));
			vo.addSemesterInfo(semesterService.getById(item.getSemesterId()));
			voList.add(vo);
		}
		return ControllerHelper.successResponse(voList);
	}

	@PostMapping("")
	@ResponseBody
	public Map<String, Object> add(@RequestBody ProjectPhaseDO phase) {
		if (null == phase) {
			return (ControllerHelper.errorResponse(1, "非法输入：要添加的助学阶段为空对象。"));
		}

		projectPhaseService.insert(phase.getProjectId(), phase.getSemesterId());
		return ControllerHelper.successResponse(null, "");
	}

}
